<template>
  <div>
    <!-- 标题 -->
    <van-nav-bar title="黑马头条 - 登录" />
    <!-- from表单 -->
    <div>
      <!-- autocomplete="off" -->
      <van-form @submit="onSubmit">
        <van-field
          v-model="user.mobile"
          required
          name="mobile"
          label="手机号"
          placeholder="请输入11位手机号"
          :rules="[
            {
              required: true,
              message: '请填写手机号',
              pattern: /^1[3-9]\d{9}$/,
            },
          ]"
        />
        <van-field
          v-model="user.code"
          type="password"
          required
          name="code"
          label="密码"
          placeholder="请输入6位密码"
          :rules="[
            { required: true, message: '请填写密码', pattern: /^\d{6}$/ },
          ]"
        />
        <div style="margin: 16px">
          <van-button
            round
            block
            type="info"
            native-type="submit"
            :disabled="isLogin"
            :loading="isLogin"
            loading-text="登录中..."
            >提交</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { loginAPI } from '@/api'
// import { Notify } from 'vant'
import Notify from '@/ui/Notify.js'
import { setToken } from '@/utils/token.js'
import { setStorage } from '@/utils/storage.js'
export default {
  data () {
    return {
      user: {
        mobile: '17513206619',
        code: '246810'
      },
      isLogin: false
    }
  },
  methods: {
    async onSubmit (values) {
      this.isLogin = true
      try {
        const res = await loginAPI(this.user)
        Notify({ type: 'success', message: '登录成功了' })
        setToken(res.data.data.token)
        setStorage('refresh_token', res.data.data.refresh_token)
        this.$router.replace({
          path: this.$route.query.path || '/layout/home'
        })
      } catch (err) {
        Notify({ type: 'danger', message: '手机号或验证码错误' })
      }
      this.isLogin = false
    }
  }
}
</script>

<style scoped lang="less">
// .van-nav-bar {
//   background-color: #007bff;
// }
// /deep/ .van-nav-bar__title {
//   color: #fff;
// }
</style>
